<?php

session_start();

require('Models/connectBDD.php');
require('Models/basics.php');
require('Models/alert.php');
require('Models/IntermedPDF.php');
require('FPDF/TFPDF/tfpdf.php');

$connexion = PDO_BDD_connect();
$alerts = getAlerts($connexion);

$title = "Your locations";
$subTitle = "Informations";

//Si l'id de location existe et est bonne
if (isset($_GET['id']) && isint($_GET['id']) && isGoodAlert($alerts,$_GET['id'],"rdv"))
{
	//On charge la location qui possède cet ID
	$locations = getLocationById($connexion,intval($_GET['id']));
}
//Sinon on charge toutes les locations de l'intermédiaire
else
{
	$locations = getLocationByInt($connexion);
}

//Instanciation of inherited class
$pdf = new tFPDF();

$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
$pdf->AddFont('DejaVu','B','DejaVuSans-Bold.ttf',true);
$pdf->AddPage();
$pdf->SetFont('DejaVu','BU',16);
$pdf->Cell(0,5,$title,0,1,'C');
$pdf->SetFont('DejaVu','',12);
$pdf->Cell(0,5,$subTitle,0,1,'C');
$pdf->SetFont('DejaVu','',11);
$pdf->Ln(10);
$pdf->SetFont('DejaVu','',10);

if (empty($locations))
{
	$pdf->MultiCell(0,4,"Vous n'avez aucun rendez-vous.");
}
else
{
	foreach ($locations as $location)
	{
		
		$pdf->MultiCell(0,4,
		html_entity_decode("Vous avez rendez-vous le ".$location->date_debut." avec ".$location->civilite." ".$location->name." ".$location->prenom.
		"\n à la résidence ".$location->nom." (".$location->numero. " ".$location->rue_1.$location->rue_2.$location->rue_3." ".$location->cp." ".$location->ville." ".$location->pays.")",ENT_NOQUOTES,"UTF-8"),0,'C');
		$pdf->Ln(4);
		$pdf->MultiCell(0,4,"",0,'C');

		$date_fin = strtotime($location->date_debut);
		$date_fin = mktime(date('H',$date_fin),date('i',$date_fin),date('s',$date_fin),date('m',$date_fin),date('d',$date_fin)+$location->duree,date('Y',$date_fin));
		$pdf->MultiCell(0,4,
		html_entity_decode("Vous avez rendez-vous le ".date('Y-m-d H:i:s',$date_fin)." avec ".$location->civilite." ".$location->name." ".$location->prenom.
		"\n à la résidence ".$location->nom." (".$location->numero. " ".$location->rue_1.$location->rue_2.$location->rue_3." ".$location->cp." ".$location->ville." ".$location->pays.")",ENT_NOQUOTES,"UTF-8"),0,'C');
		$pdf->Ln(4);
		$pdf->MultiCell(0,4,"",0,'C');

	}
}

$pdf->Output();
?>
